package com.comum.javahomework420242025.controller;

import com.comum.javahomework420242025.mapper.ClientComeMapper;
import com.comum.javahomework420242025.pojo.*;
import com.comum.javahomework420242025.repository.ClientComeRepository;
import com.comum.javahomework420242025.repository.UserRepository;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ClientComeController {
    @Autowired
    private ClientComeRepository clientComeRepository;
    @Autowired
    private ClientComeMapper clientComeMapper;
    @PostMapping("/addClientCome")
    public Result addClientCome(@RequestBody ClientCome clientCome){
        System.out.println(clientCome);
        clientCome.setId(null);
        clientComeRepository.save(clientCome);
        return Result.success();
    }
    @PutMapping("/updateClientCome")
    public Result updateClientCome(@RequestBody ClientCome clientCome){
        System.out.println(clientCome);
        clientComeRepository.save(clientCome);
        return Result.success();
    }
    @PostMapping("/getClientCome")
    public Result<PageBean<ClientComeForResult>> getClientCome(@RequestBody AskList askList){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        PageBean<ClientComeForResult> pb = new PageBean<>();
        //开启分页查询 ,PageHelper是个插件
        PageHelper.startPage(pageNum,pageSize);
        System.out.println(askList);
        List<ClientComeForResult> as = clientComeMapper.listpart(str);
        System.out.println(as);
        //Page中提供了方法，可以获取PageHelper分页查询后  得到的总记录条数和当前页数据
        com.github.pagehelper.Page<ClientComeForResult> p = (com.github.pagehelper.Page<ClientComeForResult>) as;
        //把数据填充到PageBean对象中
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        System.out.println(pb);
        return Result.success(pb);
    }
    @GetMapping("/getClientsComesByClientCompanyid")
    public Result<List<ClientComeForResult>>getClientsComesByClientCompanyid(
            String str, String clientcompanyid){
        List<ClientComeForResult> res = clientComeMapper.getClientsComesByClientCompanyid(str,clientcompanyid);
        return Result.success(res);
    }
    @DeleteMapping("/deleteClientCome")
    public Result deleteClientCome(String id){
        System.out.println(id);
        clientComeRepository.deleteById(Long.valueOf(id));
        return Result.success();
    }
}
